<div style="min-height: 600px; height: auto;" id="test1" class="demo-tree demo-tree-box layui-font-18"></div>
<script src="/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    Dcat.ready(function () {
        Array.prototype.remove = function(val) { 
            var index = this.indexOf(val); 
            if (index > -1) { 
                this.splice(index, 1); 
            } 
        };
        var initCheckedIds = {!! $checkedIds !!}
        var checkedIds = new Array()

        layui.use(['tree', 'util'], function(){
            var tree = layui.tree,
                layer = layui.layer,
                util = layui.util,
                data1 = {!! $menu !!}
          //常规用法
          tree.render({
            elem: '#test1' //默认是点击节点可进行收缩
            ,data: data1
            ,isJump: true
            ,id: 'demoId'
            ,showCheckbox: true
            ,click: function(obj){
                if (obj.state == 'open') {
                    
                    // let url = "{{ URL::asset('admin/hardware-realtime') }}/"+obj.data.id;
                    // window.location.href=url
                }
                console.log(obj.data); //得到当前点击的节点数据
                console.log(obj.state); //得到当前节点的展开状态：open、close、normal
                console.log(obj.elem); //得到当前节点元素
                
                console.log(obj.data.children); //当前节点下是否有子节点
              }
            ,oncheck: function(obj){
                //父级被勾选
                if (obj.data.children) {
                    if (obj.data.children.length) {
                        let p1 = obj.data.children
                        for (var i = 0; i<p1.length; i++) {
                            if (p1[i].children) {
                                if (p1[i].children.length) {
                                    let p2 = p1[i].children
                                    for(var j = 0; j<p2.length; j++){
                                        setCheckedIds(obj.checked, p2[j].id)
                                    }
                                }
                            } else {
                                setCheckedIds(obj.checked, p1[i].id)
                            }
                        }
                    }
                } else {
                    setCheckedIds(obj.checked, obj.data.id)
                }

                //初始化
                if (initCheckedIds.length > 0) {
                    initCheckedIds.remove(obj.data.id)
                    console.log("init")
                //手动点
                } else {
                    console.log("hand do")
                    let ids = JSON.stringify(checkedIds);
                    let url = "{{ URL::asset('admin/cabinet-compare-second') }}/"+ids;
                    window.location.href=url

                }
                
                // console.log(obj.data); //得到当前点击的节点数据
                // console.log(obj.checked); //得到当前节点的展开状态：open、close、normal
                // console.log(obj.elem); //得到当前节点元素
              }
          });
          tree.setChecked('demoId', initCheckedIds);
        });

        function setCheckedIds(checked, id){
            if (checked) {
                if (checkedIds.indexOf(id) < 0) {
                    checkedIds.push(id)
                }
            } else {
                checkedIds.remove(id)
            }
        }
    });
</script>